package com.ruoyi.common.utils.tt;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

public class FileCopyUtils {

    /**
     * 执行文件复制操作
     * @param sourceBasePath 源文件夹基础路径
     * @param projectBasePath 目标项目基础路径
     */
    public static void copyProjectFiles(String sourceBasePath, String projectBasePath) {
        try {
            // 复制Java代码文件
            copyDirectory(sourceBasePath + "/main/java/com/ruoyi/kiku/controller",
                    projectBasePath + "/ruoyi-admin/src/main/java/com/ruoyi/web/controller/kiku");

            // 复制domain、service、mapper文件
            String systemPath = projectBasePath + "/ruoyi-system/src/main/java/com/ruoyi/kiku";
            copyDirectory(sourceBasePath + "/main/java/com/ruoyi/kiku/domain", systemPath + "/domain");
            copyDirectory(sourceBasePath + "/main/java/com/ruoyi/kiku/service", systemPath + "/service");
            copyDirectory(sourceBasePath + "/main/java/com/ruoyi/kiku/mapper", systemPath + "/mapper");

            // 复制XML映射文件
            copyDirectory(sourceBasePath + "/main/resources/mapper/kiku",
                    projectBasePath + "/ruoyi-system/src/main/resources/mapper/kiku");

            // 复制Vue相关文件
            copyDirectory(sourceBasePath + "/vue/api/kiku",
                    projectBasePath + "/ruoyi-ui/src/api/kiku");
            copyDirectory(sourceBasePath + "/vue/views/kiku",
                    projectBasePath + "/ruoyi-ui/src/views/kiku");

            System.out.println("文件复制完成！");
        } catch (IOException e) {
            System.err.println("复制文件时出错: " + e.getMessage());
        }
    }

    /**
     * 复制整个目录及其内容
     * @param source 源目录路径
     * @param target 目标目录路径
     * @throws IOException
     */
    private static void copyDirectory(String source, String target) throws IOException {
        Path sourcePath = Paths.get(source);
        Path targetPath = Paths.get(target);

        if (!Files.exists(targetPath)) {
            Files.createDirectories(targetPath);
        }

        Files.walk(sourcePath).forEach(sourceFile -> {
            try {
                Path targetFile = targetPath.resolve(sourcePath.relativize(sourceFile));
                if (Files.isDirectory(sourceFile)) {
                    Files.createDirectories(targetFile);
                } else {
                    Files.copy(sourceFile, targetFile, StandardCopyOption.REPLACE_EXISTING);
                }
            } catch (IOException e) {
                System.err.println("复制 " + sourceFile + " 时出错: " + e.getMessage());
            }
        });
    }

    // 使用示例
    public static void main(String[] args) {
        String sourcePath = "C:/Users/48097/Downloads/ruoyi (5)";
        String targetPath = "D:/develop/Projects/fensu-local";
        copyProjectFiles(sourcePath, targetPath);
    }
}
